#!/bin/bash

# This file is part of util-linux.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
TS_TOPDIR="${0%/*}/../.."
TS_DESC="highlighting"
FILES="$TS_TOPDIR/ts/hexdump/files"
OPTS="--color=always"
ADDRFMT='-e "%07.7_Ax\n"'
#sample input consists of hexdump-ed results of the following py3script:
#for i in range(256):
#	print(chr(i), end= ' ')

. "$TS_TOPDIR"/functions.sh
ts_init "$*"

ts_check_test_command "$TS_CMD_HEXDUMP"
ts_check_test_command "$TS_HELPER_SYSINFO"

# on big endian systems some of the subtests have different expected output
BYTE_ORDER=$($TS_HELPER_SYSINFO byte-order)
BE_EXT=$(test "$BYTE_ORDER" = "BE" && echo ".BE")

#
# basic formats /w some highlighting tweaks
#

# highlight 'A' and 'a'
ts_init_subtest "1b_octal-1"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:A,red:a] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight bytes with the value of 0x41 (A) and 0x61 (a)
ts_init_subtest "1b_octal-2"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:0x41,red:0x61] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight the bytes at offsets 130 and  194 (decimal)
ts_init_subtest "1b_octal-3"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red@130,red@194] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight the byte at offset 194 if it's value is 0x61 (a)
# and the byte at offset 130 if it's value is A (0x41)
ts_init_subtest "1b_octal-4"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:A@130,red:0x61@194] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight the byte at offset 194 if it's value is 0x61 (a)
# and the byte at offset 130 if it's value is C (0x41) (false)
ts_init_subtest "1b_octal-5"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:C@130,red:0x61@194] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight a range longer than the byte count of %o
ts_init_subtest "1b_octal-6"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red@193-194] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight bytes with the value of 0101 (A) and 0141 (a)
ts_init_subtest "1b_octal-7"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:0101,red:0141] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight bytes with the value of 0101 (A), 0x61 (a), or if the character is a caret
ts_init_subtest "1b_octal-8"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:0101,blue:0x61,green:^] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight bytes with the value of 0101 (A), 0x61 (a), or if the character at 196 is not 'c'
ts_init_subtest "1b_octal-9"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:0101,blue:0x61,!green:c@196] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

#color the addresses red and the characters in UTIL-LINUX green
ts_init_subtest "1b_char-1"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax_L[red] " 16/1 "%3_c_L[green:-,green:I,green:L,green:N,green:T,green:U,green:X] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

#color the address 0xe0 red
ts_init_subtest "1b_char-2"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax_L[red:0xe0] " 16/1 "%3_c " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

#highlight ':' and '@'
ts_init_subtest "1b_char-3"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%3_c_L[red::@116,red:@@128] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color last address gray, the null byte blue, the spaces brown(-ish) and the text cyan
ts_init_subtest "canon-1"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS \
	-e '"%08.8_Ax_L[gray]\n"' \
	-e '"%08.8_ax  " 8/1 "%02x_L[blue:0x0,brown:0x20] " "  " 8/1 "%02x_L[brown:0x20] " ' \
	-e '"  |" 16/1 "%_p_L[cyan]" "|\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color last address gray - if it's 0x280 or blue if it's 0x380
# color A/a hex dumps blue and A/a in text green
ts_init_subtest "canon-2"
$TS_CMD_HEXDUMP $OPTS \
	-e '"%08.8_Ax_L[blue:0x380,red:0x280]\n"' \
	-e '"%08.8_ax  " 8/1 "%02x_L[blue:A,blue:a] " "  " 8/1 "%02x_L[blue:A,blue:a] " ' \
	-e '"  |" 16/1 "%_p_L[green:A,green:a]" "|\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if one of the bytes it prints is at offset 100 (0x64)
ts_init_subtest "2b_dec-1"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red@100] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if of the bytes it prints are at offsets 98 and 99
ts_init_subtest "2b_dec-2"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red@98-99] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if some or all of the bytes it prints are at offsets 98 and 99
ts_init_subtest "2b_dec-3"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red@97-99] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if it prints "A " at 130-131
ts_init_subtest "2b_dec-4"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red:A @130-131] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if it doesn't print "B " at 130-131
ts_init_subtest "2b_dec-5"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[!red:B @130-131] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# specify a string shorter than the range - error
ts_init_subtest "2b_dec-6"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red:A@130-131] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# specify a negative-length range
ts_init_subtest "2b_dec-7"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red:A@131-130] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if of the bytes it prints are at offsets 98-102 (multiple print units)
ts_init_subtest "2b_dec-8"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red@98-102] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if some or all of the bytes it prints are at offsets 97 and 99 (true)
ts_init_subtest "4b_dec-1"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red@97-99] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if some or all of the bytes it prints are at offsets 96-99
ts_init_subtest "4b_dec-2"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red@96-99] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# look for @@@@
ts_init_subtest "4b_dec-3"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@] " "\n"' \
	&> $TS_OUTPUT <<< "@@@@"
ts_finalize_subtest

# look for @@@@ at 0-3
ts_init_subtest "4b_dec-4"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@@0-3] " "\n"' \
	&> $TS_OUTPUT <<< "@@@@"
ts_finalize_subtest

# look for @@@ at 0-3 (wrong byte count error)
ts_init_subtest "4b_dec-5"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@0-3] " "\n"' \
	&> $TS_OUTPUT <<< "@@@@"
ts_finalize_subtest

# look for @@@ at 1-3 - in the format boundaries - correct
ts_init_subtest "4b_dec-6"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@1-3] " "\n"' \
	&> $TS_OUTPUT <<< "@@@@"
ts_finalize_subtest

# look for @@@ at 2-4 - correct range length, however steps over 2 print units - no colors
ts_init_subtest "4b_dec-7"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@2-4] " "\n"' \
	&> $TS_OUTPUT <<< "@@@@"
ts_finalize_subtest

ts_finalize
